/*
   Copyright 2009 Bégaudeau Stéphane

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */

package faneclipse.views;

import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;

/**
 * @author BEGAUDEAU stephane
 */
public class FanContentProvider implements ITreeContentProvider {
	/**
	 * The ContentOutlinePage.
	 */
	private FanContentOutlinePage fanContentOutlinePage;
	
	/**
	 * The constructor.
	 * @param fanContentOutlinePage The ContentOutlinePage.
	 */
	public FanContentProvider(final FanContentOutlinePage fanContentOutlinePage) {
		this.fanContentOutlinePage = fanContentOutlinePage;
	}

	/**
	 * @param v The viewer.
	 * @param oldInput The old input.
	 * @param newInput The new input.
	 * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
	 */
	public void inputChanged(final Viewer v, final Object oldInput, final Object newInput) {
	}

	/**
	 * @see org.eclipse.jface.viewers.IContentProvider#dispose()
	 */
	public final void dispose() {
	}

	/**
	 * @param parent The parent object.
	 * @return The children of the object.
	 * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
	 */
	public final Object[] getElements(final Object parent) {
		if (parent.equals(this.fanContentOutlinePage.getEditorInput())) {
			if (fanContentOutlinePage.getInvisibleRoot() == null)
			{
				fanContentOutlinePage.initialize();
			}
			return getChildren(fanContentOutlinePage.getInvisibleRoot());
		}
		return getChildren(parent);
	}

	/**
	 * @param child The child.
	 * @return The parent.
	 * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
	 */
	public final Object getParent(final Object child) {
		if (child instanceof TreeObject)
		{
			return ((TreeObject) child).getParent();
		}
		return null;
	}

	/**
	 * @param parent The parent.
	 * @return The child.
	 * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
	 */
	public final Object[] getChildren(final Object parent) {
		if (parent instanceof TreeParent)
		{
			return ((TreeParent) parent).getChildren();
		}
		return new Object[0];
	}

	/**
	 * @param parent The parent.
	 * @return A boolean showing if the object has children.
	 * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object)
	 */
	public final boolean hasChildren(final Object parent) {
		if (parent instanceof TreeParent)
		{
			return ((TreeParent) parent).hasChildren();
		}
		return false;
	}
}
